using System.Collections.Generic;
using UnityEngine;
using System;

public class AudioClipCutter : MonoBehaviour
{
    public AudioClip originalClip; 
    public float clipDuration ;
    public int everyTime;
    public int numSegments;
    public float segmentDuration;
    public List<AudioClip> audioClips;
    public AudioSource source;
    

    public void Cutter(AudioClip originalClip)
    {
        audioClips.Clear();   
        if (originalClip == null)
        {
            Debug.LogError("Original AudioClip is not assigned!");
            return;
        }

        // Calculate number of segments
        clipDuration= originalClip.length;
        numSegments = (int)Math.Ceiling( clipDuration / everyTime);
        segmentDuration = (clipDuration / numSegments);

        // Cut the AudioClip into segments
        for (int i = 0; i < numSegments; i++)
        {
            float startTime = i * segmentDuration;
            float endTime = startTime + segmentDuration;

            if (endTime > originalClip.length)
                endTime = originalClip.length;

            // Create new AudioClip
            AudioClip segmentClip = AudioClip.Create(
                "Segment_" + i.ToString(),
                (int)((endTime - startTime) * originalClip.frequency),
                originalClip.channels,
                originalClip.frequency,
                false);
            
            // Set data for the new AudioClip
            float[] data = new float[(int)((endTime - startTime) * originalClip.frequency * originalClip.channels)];
            originalClip.GetData(data, (int)(startTime * originalClip.frequency * originalClip.channels));
            segmentClip.SetData(data, 0);
            audioClips.Add(segmentClip);
            // Save or use the segmentClip as needed
            // For demonstration, let's just log their names
            Debug.Log("Segment " + i.ToString() + ": " + segmentClip.name);
        }
    }
    public void PlayClip(int a)
    {
        source.clip = audioClips[a];
        source.Play();
    }


    public void kjkj()
    {
        Cutter(originalClip);
    }
    
    
}
